﻿using ManagementApi.Models.Manage.ProjectGroup;
using Modules.Constructor;
using Modules.Corp;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using Modules.System.User;
using Modules.Common;
using Modules.Project;

namespace ManagementApi.Models.Manage.ProjectCorp
{
    public class ProjectCorpDto : OperateDto
    {
        /// <summary>
        /// id
        /// </summary>
        public int? Id { get; set; }

        /// <summary>
        /// 参建单位企业Id
        /// </summary>
        public int? CorpId { get; set; }

        //public 项目参建单位类型 Role { get; set; }

        /// <summary>
        /// 参建单位类型
        /// </summary>
        public string RoleName { get; set; }

        /// <summary>
        /// 承建状态
        /// </summary>
        public 承建状态? ConstructionStatus { get; set; }

        /// <summary>
        /// 承建状态
        /// </summary>
        public string? ConstructionStatusName { get; set; }

        /// <summary>
        /// 与总包签订合同金额(元)
        /// </summary>
        public decimal ContractAmount { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 入场时间
        ///</summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 离场时间
        ///</summary>
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 负责人名称
        /// </summary>
        public string? LeaderName { get; set; }

        /// <summary>
        /// 负责人名称
        /// </summary>
        public string? LeaderCardno { get; set; }

        /// <summary>
        /// 负责人名称
        /// </summary>
        public string? LeaderPhoneNum { get; set; }

        /// <summary>
        /// 项目管理部编码
        /// </summary>
        public string? ProjectManageCode { get; set; }

        /// <summary>
        /// 承建内容
        /// </summary>
        public string? BuildContent { get; set; }

        /* 以下字段为其他表中的数据*/

        /// <summary>
        /// 项目名称
        /// </summary>
        public string PrjName { get; set; }

        /// <summary>
        /// 项目编号
        /// </summary>
        public string PrjNum { get; set; }

        /// <summary>
        /// 参建单位名称
        /// </summary>
        public string CorpName { get; set; }

        /// <summary>
        /// 参建单位统一社会信用代码
        /// </summary>
        public string CorpCode { get; set; }

        /// <summary>
        /// 单位类型
        /// </summary>
        public ConstructorType Type { get; set; }

        /// <summary>
        /// 单位类型
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// 法人民名称
        /// </summary>
        public string? LegalMan { get; set; }

        /// <summary>
        /// 法人身份证号
        /// </summary>
        public string? LegalCardno { get; set; }

        /// <summary>
        /// 法人手机号
        /// </summary>
        public string? LegalMobile { get; set; }

        /// <summary>
        /// 企业联系人
        /// </summary>
        public string LinkMan { get; set; }

        /// <summary>
        /// 企业联系人电话
        /// </summary>
        public string LinkTel { get; set; }
    }

    public static partial class ProjectCorpExceptions
    {
        public static ProjectCorpDto AsDto(this Modules.Project.Entitys.ProjectCorp projectCorp, ProjectInfo projectInfo, IApplicationContext applicationContext)
        {
            ProjectCorpDto projectCorpDto = new();

            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator() || projectInfo?.DeptId == deptUser.DeptId)
                    projectCorpDto.HasEdit = true;
                else projectCorpDto.HasEdit = false;
            }

            #endregion 主管单位数据权限

            projectCorpDto.ConstructionStatus = projectCorp.ConstructionStatus;
            projectCorpDto.ConstructionStatusName = projectCorp.ConstructionStatus.GetDescription();
            projectCorpDto.Id=projectCorp.Id;
            projectCorpDto.CorpId = projectCorp.CorpId;
            projectCorpDto.RoleName = projectCorp.Role.GetDescription();
            projectCorpDto.PrjName = projectInfo.Name;
            projectCorpDto.PrjNum = projectInfo.PrjNum;
            CorpInfo? corpInfo = applicationContext.GetService<CorpService>().GetCorpInfo(projectCorp.CorpId);
            if (corpInfo == null) return projectCorpDto;
            projectCorpDto.CorpName = corpInfo.Name;
            projectCorpDto.CorpCode = corpInfo.Code;
            projectCorpDto.Type = corpInfo.Type;
            projectCorpDto.LinkMan = corpInfo.LinkMan;
            projectCorpDto.LinkTel = corpInfo.LinkTel;
            projectCorpDto.LegalMan = corpInfo.Legalman;
            projectCorpDto.LegalCardno = corpInfo.LegalCardno;
            projectCorpDto.LegalMobile = corpInfo.LegalMobile;
            projectCorpDto.TypeName = projectCorpDto.Type.GetDescription();

            return projectCorpDto;
        }

        public static ProjectCorpDto AsDto(this CorpInfo corpInfo, ProjectInfo projectInfo, IApplicationContext applicationContext, string roleName)
        {
            ProjectCorpDto projectCorpDto = new();

            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator() || projectInfo?.DeptId == deptUser.DeptId)
                    projectCorpDto.HasEdit = true;
                else projectCorpDto.HasEdit = false;
            }

            #endregion 主管单位数据权限
            projectCorpDto.CorpId = corpInfo.CorpId;
            projectCorpDto.RoleName = roleName;
            projectCorpDto.ConstructionStatusName = "进场";
            projectCorpDto.PrjName = projectInfo.Name;
            projectCorpDto.PrjNum = projectInfo.PrjNum;
            projectCorpDto.CorpName = corpInfo.Name;
            projectCorpDto.CorpCode = corpInfo.Code;
            projectCorpDto.Type = corpInfo.Type;
            projectCorpDto.LinkMan = corpInfo.LinkMan;
            projectCorpDto.LinkTel = corpInfo.LinkTel;
            projectCorpDto.LegalMan = corpInfo.Legalman;
            projectCorpDto.LegalCardno = corpInfo.LegalCardno;
            projectCorpDto.LegalMobile = corpInfo.LegalMobile;
            projectCorpDto.TypeName = projectCorpDto.Type.GetDescription();

            return projectCorpDto;
        }

        /// <summary>
        /// 建设单位专户为项目参与企业dto
        /// </summary>
        /// <param name="constructor"></param>
        /// <param name="projectInfo"></param>
        /// <param name="applicationContext"></param>
        /// <returns></returns>
        public static ProjectCorpDto AsDto(this ConstructorInfo constructor, ProjectInfo projectInfo, IApplicationContext applicationContext)
        {
            ProjectCorpDto projectCorpDto = new();

            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator() || projectInfo?.DeptId == deptUser.DeptId)
                    projectCorpDto.HasEdit = true;
                else projectCorpDto.HasEdit = false;
            }

            #endregion 主管单位数据权限

            projectCorpDto.RoleName = "建设单位";
            projectCorpDto.ConstructionStatusName = "进场";
            projectCorpDto.PrjName = projectInfo.Name;
            projectCorpDto.PrjNum = projectInfo.PrjNum;
            projectCorpDto.CorpName = constructor.Name;
            projectCorpDto.CorpCode = constructor.Code;
            projectCorpDto.Type = constructor.Type;
            projectCorpDto.LinkMan = constructor.LinkMan;
            projectCorpDto.LinkTel = constructor.LinkTel;
            projectCorpDto.LegalMan = constructor.Legalman;
            projectCorpDto.LegalCardno = constructor.LegalCardno;
            projectCorpDto.LegalMobile = constructor.LegalMobile;
            projectCorpDto.TypeName = constructor.Type.GetDescription();

            return projectCorpDto;
        }

        //public static ProjectCorpDto AsProjectCorpDto(this Modules.Project.Entitys.ProjectCorp projectCorp, ProjectInfo projectInfo, IApplicationContext applicationContext)
        //{
        //    ProjectCorpDto projectCorpDto = new ProjectCorpDto();
        //    applicationContext.GetService<IMapper>().Map(projectCorp, projectCorpDto);
        //    CorpService corpService = applicationContext.GetService<CorpService>();
        //    ConstructorService constructorInfoService = applicationContext.GetService<ConstructorService>();
        //    projectCorpDto.RoleName = projectCorp.Role.GetDescription();
        //    projectCorpDto.ConstructionStatusName = projectCorpDto.ConstructionStatus?.GetDescription();
        //    projectCorpDto.PrjName = projectInfo.Name;
        //    projectCorpDto.PrjNum = projectInfo.PrjNum;

        //    #region 主管单位数据权限

        //    if (applicationContext.CurrentUser is IDeptUser deptUser)
        //    {
        //        if (deptUser.IsSuperAdministrator() || projectInfo?.DeptId == deptUser.DeptId)
        //            projectCorpDto.HasEdit = true;
        //        else projectCorpDto.HasEdit = false;
        //    }

        //    #endregion 主管单位数据权限

        //    CorpInfo? corpInfo = corpService.GetCorpInfo(projectCorp.CorpId);
        //    if (corpInfo == null) return projectCorpDto;
        //    projectCorpDto.CorpName = corpInfo.Name;
        //    projectCorpDto.CorpCode = corpInfo.Code;
        //    projectCorpDto.Type = corpInfo.Type;
        //    projectCorpDto.LinkMan = corpInfo.LinkMan;
        //    projectCorpDto.LinkTel = corpInfo.LinkTel;
        //    projectCorpDto.LegalMan = corpInfo.Legalman;
        //    projectCorpDto.LegalCardno = corpInfo.LegalCardno;
        //    projectCorpDto.LegalMobile = corpInfo.LegalMobile;

        //    projectCorpDto.TypeName = projectCorpDto.Type.GetDescription();

        //    return projectCorpDto;
        //}

        //public static IEnumerable<ProjectCorpDto> AsProjectCorpDtos(this IEnumerable<Modules.Project.Entitys.ProjectCorp> projectCorps, IEnumerable<CorpInfo> corps)
        //{
        //    List<ProjectCorpDto> projectCorpDtos = new();

        //    foreach (var item in projectCorps)
        //    {
        //        var corp = corps.Where(a => a.CorpId == item.CorpId).FirstOrDefault();

        //        ProjectCorpDto corpDto = new()
        //        {
        //            RoleName = item.Role.GetDescription(),
        //            ContractAmount = item.ContractAmount,
        //            CreateTime = item.CreateTime,
        //            Id = item.Id,
        //            StartDate = item.StartDate,
        //            EndDate = item.EndDate
        //        };
        //        if (corp != null)
        //        {
        //            corpDto.CorpName = corp.Name;
        //            corpDto.CorpId = corp.CorpId;
        //            corpDto.LinkMan = corp.LinkMan;
        //            corpDto.LinkTel = corp.LinkTel;
        //        }
        //        projectCorpDtos.Add(corpDto);
        //    }
        //    return projectCorpDtos;
        //}

        //public static string AsCorpName(this Modules.Project.Entitys.ProjectCorp projectCorp)
        //{
        //    CorpInfo corpInfo = new CorpInfo();
        //    corpInfo.CorpId = (int)projectCorp.CorpId;
        //    return corpInfo.GetCorpName();
        //}

        ///// <summary>
        ///// 获取企业名称
        ///// </summary>
        ///// <param name="corpInfo"></param>
        ///// <returns></returns>
        //public static string GetCorpName(this CorpInfo corpInfo)
        //{
        //    var corpService = ServiceAccessor.GetService<CorpService>();
        //    var corp = corpService.GetCorpInfo(corpInfo.CorpId);
        //    if (corp == null)
        //        return "";
        //    return corp.Name;
        //}

        /// <summary>
        /// 转为完整树结构
        /// </summary>
        /// <param name="projectCorps"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public static IEnumerable<VueTree<int>> AsVueTree(this IEnumerable<Modules.Project.Entitys.ProjectCorp> projectCorps, int projectId = 0)
        {
            List<VueTree<int>> vueTrees = new();
            if (!projectCorps.Any()) return vueTrees;
            if (projectId == 0) projectId = projectCorps.First().ProjectId;
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var projectGroups = projectGroupService.GetProjectGroupsByCordProjectId(projectId, projectCorps.Select(a => a.CorpId));
            var corpService = ServiceAccessor.GetService<CorpService>();
            var corps = corpService.GetCorpsByCorpIds(projectCorps.Select(a => a.CorpId));

            foreach (var projectGroup in projectGroups)
            {
                var children = projectGroups.Where(i => i.ProjectId == projectGroup.ProjectId && i.CorpId == projectGroup.CorpId).AsVueTree();
                VueTree<int> vueTree = new()
                {
                    Label = corps.FirstOrDefault(i => i.CorpId == projectGroup.CorpId)?.Name ?? string.Empty,
                    Id = projectGroup.CorpId,
                    Extend1 = "Corp",
                    Icon = "address-card",
                    Value = projectGroup.CorpId,
                    Children = children,
                    leaf = !children.Any()
                };
                vueTrees.Add(vueTree);
            }
            return vueTrees;
        }
    }
}